<%@ page contentType="text/html; charset=iso-8859-1" language="java" %>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.sql.ResultSet"%>

<%
  String username = request.getParameter("q");
  String password = request.getParameter("w");
  //out.print(username+" "+password);
%>

<%
  try {
    /*
     * Create string of connection url within specified format with machine
     * name, port number and database name. Here machine name id localhost and
     * database name is usermaster.
     */
    String connectionURL = "jdbc:mysql://localhost:3306/progin_171_13509024";

// declare a connection by using Connection interface 
    Connection connection = null;

// Load JBDC driver "com.mysql.jdbc.Driver" 
    Class.forName("com.mysql.jdbc.Driver").newInstance();

    /*
     * Create a connection by using getConnection() method that takes parameters
     * of string type connection url, user name and password to connect to
     * database.
     */
    connection = DriverManager.getConnection(connectionURL, "progin", "progin");

// check weather connection is established or not by isClosed() method 


    Statement st = connection.createStatement();
    ResultSet resultSet = st.executeQuery("SELECT * FROM tbl_user WHERE username = '" + username + "' AND password = '" + password + "';");

    if (resultSet.next()) {
      Cookie userCookie = new Cookie("username", resultSet.getString(1));
      userCookie.setMaxAge(10);
      response.addCookie(userCookie);
      out.write("YES");

    } else {
      out.write("NO");
    }

    connection.close();
  } catch (Exception ex) {
    out.print("GAGAL");
  }
%>
